﻿using System;
using System.Web.Mvc;
using HRMS_Model;
using System.Diagnostics;
using System.Linq;

namespace HRMS.Models.ModelBinders
{
    public class CustomModelBinder : DefaultModelBinder
    {
        public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            var model = base.BindModel(controllerContext, bindingContext);

            if (controllerContext.HttpContext.Request.Params["id"] != "_empty")
            {
                var id = Convert.ToInt32(controllerContext.HttpContext.Request.Params["id"]);

                if (((System.Reflection.MemberInfo)(bindingContext.ModelType)).Name == "Role")
                {
                    var rolemodel = (Role)model;
                    rolemodel.RoleId = id;
                    model = rolemodel;
                }
                else if (((System.Reflection.MemberInfo)(bindingContext.ModelType)).Name == "Employee")
                {
                    Designation designation;
                    Department department;
                    var designationName = controllerContext.HttpContext.Request.Params["DesignationName"].ToString();
                    var departmentName = controllerContext.HttpContext.Request.Params["DepartmentName"].ToString();
                    using (var entity = new HRMSEntities())
                    {
                        designation = entity.Designations.FirstOrDefault(d => d.DesignationName == designationName);
                        department = entity.Departments.FirstOrDefault(d => d.DepartmentName == departmentName);
                    }
                    var empModel = (Employee)model;
                    empModel.EmpId = id;
                    empModel.DesignationId = designation.DesignationId;
                    empModel.DepartmentId = department.DepartmentId;
                    model = empModel;
                }
                else if (((System.Reflection.MemberInfo)(bindingContext.ModelType)).Name == "Applicant")
                {
                    Degree degree;
                    Skill skill;
                    var degreeName = controllerContext.HttpContext.Request.Params["DegreeName"].ToString();
                    var skillName = controllerContext.HttpContext.Request.Params["SkillName"].ToString();

                    using (var entity = new HRMSEntities())
                    {
                        degree = entity.Degrees.FirstOrDefault(d => d.DegreeName == degreeName);
                        skill = entity.Skills.FirstOrDefault(s => s.SkillName == skillName);
                    }
                    var applicantModel = (Applicant)model;
                    applicantModel.ApplicantId = id;
                    applicantModel.DegreeId = degree.DegreeId;
                    applicantModel.SkillId = skill.SkillId;
                    model = applicantModel;
                }
            }
            else
            {
                if (((System.Reflection.MemberInfo)(bindingContext.ModelType)).Name == "Employee")
                {
                    Designation designation;
                    Department department;
                    var designationName = controllerContext.HttpContext.Request.Params["DesignationName"].ToString();
                    var departmentName = controllerContext.HttpContext.Request.Params["DepartmentName"].ToString();
                    using (var entity = new HRMSEntities())
                    {
                        designation = entity.Designations.FirstOrDefault(d => d.DesignationName == designationName);
                        department = entity.Departments.FirstOrDefault(d => d.DepartmentName == departmentName);
                    }
                    var empModel = (Employee)model;
                    empModel.DesignationId = designation.DesignationId;
                    empModel.DepartmentId = department.DepartmentId;
                    model = empModel;
                }
                else if (((System.Reflection.MemberInfo)(bindingContext.ModelType)).Name == "Applicant")
                {
                    Degree degree;
                    Skill skill;
                    var degreeName = controllerContext.HttpContext.Request.Params["DegreeName"].ToString();
                    var skillName = controllerContext.HttpContext.Request.Params["SkillName"].ToString();

                    using (var entity = new HRMSEntities())
                    {
                        degree = entity.Degrees.FirstOrDefault(d => d.DegreeName == degreeName);
                        skill = entity.Skills.FirstOrDefault(s => s.SkillName == skillName);
                    }
                    var applicantModel = (Applicant)model;
                    applicantModel.DegreeId = degree.DegreeId;
                    applicantModel.SkillId = skill.SkillId;
                    model = applicantModel;
                }
            }
            return model;
        }
    }
}